package com.vinfai.common.service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import com.vinfai.common.exception.AppException;
import com.vinfai.common.exception.DataAccessException;

/**
 * 通用Service层
 * @author vinfai
 *
 */
public interface IBaseService<E extends Serializable,PK extends Serializable> {
	
	public PK save(E entity) throws AppException;
	
	public void saveOrUpdate(E entity) throws AppException;
	
	public void update(E entity) throws AppException;
	
	public void merge(E entity) throws AppException;
	
	public void deleteById(PK id) throws AppException;
	
	public void delete(E entity) throws AppException;
	
	public E getById(PK id) throws AppException;

	/**
	 * 批量更新
	 * @param updateSql hibernate bluk update/delete sql String.
	 * @param params 参数Map集合
	 * @return	numbers of PO affected by update operation
	 * @throws DataAccessException
	 */
	public int bulkUpdate(String updateSql,Map<String,?> params) throws AppException;
	
	public int bulkUpdate(String updateSql) throws AppException;
	/**
	 * 更新或者删除操作，数组参数的次序需要同SQL中位置保持一致
	 * @param updateSql hibernate bluk update/delete sql String.
	 * @param params   参数数组
	 * @return numbers 更新的记录数
	 * @throws DataAccessException
	 */
	public int bulkUpdate(String updateSql,Object... params)throws AppException;
	
	public int bulkNativeUpdate(String nativeSql)throws AppException;
	/**
	 * 通过原始SQL更新操作，参数同bulkUpdate
	 * @param nativeSql
	 * @param params Map 参数通过name匹配
	 * @return
	 * @throws DataAccessException
	 */
	public int bulkNativeUpdate(String nativeSql,Map<String,?> params)throws AppException;
	
	public int bulkNativeUpdate(String nativeSql,Object... params)throws AppException;
	
	
	public <T> List<T> find(String hql,Map<String,?> params) throws AppException;
	public <T> List<T> find(String hql,Object... params) throws AppException;
	
	public <T> List<T> find(String hql,int firstResult,int maxResults,Map<String,?> params)throws AppException;
	/**
	 * 
	 * @param <T> 返回值泛型
	 * @param hql 查询HQL语句
	 * @param firstResult 开始位置 first result
	 * @param maxResults max result
	 * @param params 
	 * @return 泛型集合
	 * @throws DataAccessException
	 */
	public <T> List<T> find(String hql,int firstResult,int maxResults,Object...params)throws AppException;
	
	
	public <T> List<T> findNativeQuery(final String nativeSql,final int firstResult,final int maxResults,final Object... params) throws AppException;

	public <T> List<T> findNativeQuery(final String nativeSql,final int firstResult,final int maxResults,final Map<String,?> params) throws AppException;
	
	
}
